package com.atguigu.biliplayer.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.widget.Toast;

import com.atguigu.biliplayer.R;
import com.atguigu.biliplayer.utils.NetUtils;


/**
 * 欢迎页面
 * Created by 刘晨龙 on 2015/11/04
 */
public class WelcomeActivity extends Activity {


    /**
     * 动画是否完成
     * @author 刘晨龙
     */
    private boolean isFinishAnim = false;

    /**
     * 网络是否链接
     * @author 刘晨龙
     */
    private boolean isCheckNet = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_welcome);
        exeAnim();
        checkNet();
    }

    /**
     * 检查是都有网络链接以及类型
     * @author 刘晨龙
     */
    private void checkNet() {
        new Thread(){
            @Override
            public void run() {
                super.run();
                boolean connected = NetUtils.isConnected(WelcomeActivity.this);
                if(!connected){
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(WelcomeActivity.this, "没有可用的网络链接", Toast.LENGTH_SHORT).show();
                        }
                    });
                }else if(!NetUtils.isWifi(WelcomeActivity.this)){
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(WelcomeActivity.this, "注意：你正在使用手机流量呦~", Toast.LENGTH_SHORT).show();
                        }
                    });
                }
                isCheckNet = true;
                startMian();
            }
        }.start();
    }

    /**
     * 执行动画
     * @author 刘晨龙
     */
    private void exeAnim() {
        View welcome = findViewById(R.id.welcome_icon);
        AlphaAnimation alphaAnimation = new AlphaAnimation(0f, 1.0f);
        alphaAnimation.setDuration(1000);
        alphaAnimation.setFillAfter(true);
        welcome.setAnimation(alphaAnimation);
        alphaAnimation.start();
        alphaAnimation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                isFinishAnim = true;
                startMian();
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
    }

    /**
     * 加载主页面
     * @author 刘晨龙
     */
    private void startMian() {
        if (isCheckNet && isFinishAnim) {
            startActivity(new Intent(this, MainActivity.class));
            finish();
        }
    }
}
